define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');

	// 活动诊断
	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'url': 'campaign/info',
				'class': 'P-campaignDiagnosis'
			});
			// 默认表格请求参数
			this.$gridParam = {
				'QueryType': 'Size', // 查看维度参数
				'Complete': 0, // 尺寸状态参数
				'Verify': 0, // 审核状态参数
				'CampaignId': 0
			};
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var el = self.getContainer();
			var c = self.getConfig();
			var doms = {
				title: $('<div class="M-formHeadTitle mb20"></div>').appendTo(el),
				name:  $('<div class="P-campaignDiagnosisName mb10"><span>' + LANG("活动名称：") + '</span><span></span></div>').appendTo(el).find('span:last'),
				gridCon: $('<div class="P-campaignDiagnosisGridCon" pub-name="grid" pub-mod="campaign/grid/diagnosis.base"/>').appendTo(el)
			};

			this.buildFromTemplate({
				'grid': {
					eventDataLoad: true,
					hasFilterSidebar: true,
					filterAutoFire: true,
					sidebar_config: {
						'items': [
							{'name': 'dim', 'uri':'modules/sideBarBase.radio', 'config': {
								'title': LANG('查看维度：'),
								'data': [
									{id: 'Size', name: LANG('按广告位尺寸')},
									{id: 'AdPosition', name: LANG('按广告位')},
									{id: 'Sweety', name: LANG('按创意包')},
									{id: 'Deal', name: LANG('按订单')}
								],
								'value': 'Size',
								'compareValue': true
							}},
							{'name': 'sizeStatus', 'uri':'modules/sideBarBase.radio', 'config': {
								'title': LANG('尺寸状态：'),
								'data': [
									{id: 0, name: LANG('所有')},
									{id: 1, name: LANG('齐全')},
									{id: 2, name: LANG('部分缺少')},
									{id: 3, name: LANG('没有尺寸')}
								],
								'value': 0
							}},
							{'name': 'verifyStatus', 'uri':'modules/sideBarBase.radio', 'config': {
								'title': LANG('审核状态：'),
								'data': [
									{id: 0, name: LANG('所有')},
									{id: 1, name: LANG('全部通过')},
									{id: 2, name: LANG('部分通过')},
									{id: 3, name: LANG('没有通过')}
								],
								'value': 0
							}}
						]
					}
				}
			});
			self.$doms = doms;
		},
		//响应右边侧边栏选项
		onSidebarFilter: function( ev ) {
			var self = this;
			util.each(ev.param, function(item, idx)
			{
				var type = idx;
				var id = item.id;
				switch (type)
				{
					case 'dim':
						self.$gridParam.QueryType = id;
						break;
					case 'sizeStatus':
						self.$gridParam.Complete = +id;
						break;
					case 'verifyStatus':
						self.$gridParam.Verify = +id;
						break;
				}
			});

			self.$.grid.reload(self.$gridParam);
		},
		// 表格数据加载完之后
		onGridDataLoad: function( ev ) {
			var type = this.$gridParam && this.$gridParam.QueryType;
			this.$.grid.switchColumn( type );
			this.$.grid.setCampaignId( this.$gridParam['CampaignId'] );
			// 活动名称
			this.$doms.name.text(ev.param.campaign_name);
			return false;
		},
		reset: function() {
			return this;
		},
		setTitle: function(title) {
			this.$doms.title.text(title);
			return this;
		},
		load: function( id ) {
			this.$gridParam['CampaignId'] = id;
			// 拿到活动id后, 加载grid数据
			this.reload(this.$gridParam);
		},
		reload: function( param ) {
			this.$.grid.reload(param);
		}
	});
	exports.main = Main;


	exports.oneKey = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': '',
				'view_model':{
					'view': {}
					,'href': ''
					,'title': ''
				}
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var options = [];
			self.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('一键诊断')+'</span>',
					'<a target="_blank" ms-href="href">{{title}}</a>',
					'<div>',
					'</div>',
				'<span class="mr10 fr" pub-name="set" pub-mod="@base/common/drop.drop"></span>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="campaign/grid/diagnosis.oneKey"/>'
			].join('')));

			var config = {
				'grid': {
					param: {}
				}
				,'set':
				{
					'def': LANG('快捷设置')
					,'type': 'button'
					,'addClass': 'blue'
					,'hasSearch': false
					,'width': 120
					,'showResult': false
					,'panel_config': {
						appendTo: 'popup'
					}
					,'options': this.getSetting()
				}
			};

			self.buildFromTemplate(config);
			// 批量编辑界面窗口
			self.createAsync('infoPop', 'campaign/popwin/diagnosis.info');
			self.createAsync('batchEdit', 'campaign/popwin/batchEdit.main');
		},
		afterBuildTemplate: function(mod, param)
		{
		},
		getSetting: function()
		{
			var ret = [
				{id: 'batchEdit.price', name: LANG("修改出价")},
				{id: 'batchEdit.budget', name: LANG("修改预算")},
				{id: 'batchEdit.bidSpeed', name: LANG("投放速度")},
				{id: 'batchEdit.frequency', name: LANG("频次控制")},
				{id: 'batchEdit.character', name: LANG("人群选择")},
				{id: 'batchEdit.date', name: LANG("投放日期")},
				{id: 'batchEdit.timeset', name: LANG("日程设置")},
				{id: 'batchEdit.zone', name: LANG("投放地区")},
				{id: 'batchEdit.client', name: LANG("客户端")},
				{id: 'batchEdit.terminalType', name: LANG("终端类型")},
				{id: 'batchEdit.nSpeculator', name: LANG("溢价策略")},
				{id: 'batchEdit.smartMonitor', name: LANG("智能监控")},
				{id: 'batchEdit.nameReplace', name: LANG('活动名称关键词替换')}
			];

			if(pubjs.checkRight('campaign.amonitor'))
			{
				ret.push({id: 'batchEdit.aMonitorUrl', name: LANG('三方监测地址')});
			}
			if (pubjs.checkRight('campaign.videoDirect')) {
				ret.push({id: 'batchEdit.vOrientation', name: LANG("视频定向")});
			}


			return ret;
		},
		onDropChange: function(ev)
		{
			if(ev && ev.name === 'set')
			{
				var id = ev.param.id;
				var name = ev.param.name;
				var keys = id.split('.');
				// 判断是否PC端的，如果是PC端的弹出弹窗提示
				if(keys[1] === 'terminalType' && this.$Type !== 1)
				{
					return pubjs.alert(LANG('只有PC的才可以修改终端类型'));
				}

				this.$.batchEdit.reset().show().setTitle(name).setType(keys[1]).setParam([this.$id]);
			}
		},
		onBatchEditSuccess: function()
		{
			this.$.grid.reload();
		},
		load: function(id)
		{
			this.$id = id;
			this.$.grid.setParam({_id: +id}).reload();
		},
		// 表格数据加载完之后
		onGridDataLoad: function( ev )
		{
			var data = ev.param;
			var vm = this.vm.$;
			vm.title = `${LANG('活动对象')}:  ${this.$id}   ${data.Name}`;
			vm.href = `#campaign/${data.Channel == 1 ? 'edit' : 'MTEdit'}/${this.$id}`;
			this.$Type = data.Channel;
			return false;
		},
		// 操作菜单-下拉菜单界面
		onOperateMenuShow: function(ev){
			var param = ev.param;
			if(param.module !== 'other')
			{
				ev.returnValue = [{id: 'info', name: LANG('查看详情')}];
			}
			else
			{
				ev.returnValue = [{id: '', name: LANG('没有操作')}];
			}

			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();
			var id = this.$id;
			var module  = data.module;
			var self = this;
			var pop = this.$.infoPop;

			switch(op.key)
			{
				case 'info':
					if(pop)
					{
						pop.setModule(module).load(id).show();
					}

					break;
			}

			return false;
		}
	});
});